<?php
namespace Admin\Model;
use Think\Model\ViewModel;
class ProductModel extends ViewModel {

    Public function GetIdCount($table,$pid){
        $num = count(M("$table")->where("pid=$pid")->select());
        return $num;
    }

    Public function search($str,$arr,$Idname){
        //前提 : 字段名与数据库同名
        switch ($str){case 'size':$middle = pro_siz;break;case 'color':$middle = pro_col;break;case 'reviews':$middle = pro_rev;break;}
        //switch没有直接用参数高端,哎!!
        foreach ($arr as $k => $v){
            foreach ($v as $kk => $vv){

                if ($kk=='pid'){

                    $where1['pid'] = $vv;
                    $xid = M("$middle")->where($where1)->field("$Idname")->select();

                    $num = $this->GetIdCount("$middle","$vv");

//                    var_dump($num);

                    if ($num==0){
                        $size_arr["$k"]['0'] = '无';
                    }else{
                        for($i=0;$i<$num;$i++){     //前提:  ※要※改※固定一个商品只有两种尺码,颜色,回复
                            $where2["$Idname"] = $xid["$i"]["$Idname"];
                            $size = M("$str")->where($where2)->field("$str")->select();
                            if (empty($size)){
                                $size_arr["$k"]["$i"] = '无';
                            }else{
                                $size_arr["$k"]["$i"] = $size['0']["$str"];
                            }
                        }
                    }
                }
            }
        }
        return $size_arr;    //是个二维数组
    }
}

?>